﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace HigLabo.Net.WindowsLive
{
    /// <summary>
    /// 
    /// </summary>
    public class CreateActivityCommand : WindowsLiveCommand
    {
        /// <summary>
        /// 
        /// </summary>
        public String UserID { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public String Message { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public String Link { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public String Description { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public String Picture { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public String Name { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public override string GraphApiPath
        {
            get
            {
                if (HttpClient.IsNullOrWhiteSpace(this.UserID))
                    return "me/share";
                else
                    return this.UserID + "/share";
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public override HttpMethodName MethodName
        {
            get
            {
                return HttpMethodName.Post;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public CreateActivityCommand()
            : this("")
        {
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="userID"></param>
        public CreateActivityCommand(String userID)
        {
            this.UserID = userID;
            this.ContentType = "application/json";
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        protected override byte[] SetData()
        {
            if (HttpClient.IsNullOrWhiteSpace(Message))
                throw new ArgumentException();

            StringBuilder sb = new StringBuilder();
            sb.AppendLine("{");
            sb.AppendFormat("message: \"{0}\"", Message);
            if (!HttpClient.IsNullOrWhiteSpace(Link))
            {
                sb.Append(",\r\n");
                sb.AppendFormat("link: \"{0}\"", Link);
            }
            if (!HttpClient.IsNullOrWhiteSpace(Description))
            {
                sb.Append(",\r\n");
                sb.AppendFormat("description: \"{0}\"", Description);
            }
            if (!HttpClient.IsNullOrWhiteSpace(Picture))
            {
                sb.Append(",\r\n");
                sb.AppendFormat("picture: \"{0}\"", Picture);
            }
            if (!HttpClient.IsNullOrWhiteSpace(Name))
            {
                sb.Append(",\r\n");
                sb.AppendFormat("name: \"{0}\"", Name);
            }
            sb.Append("\r\n}");

            return Encoding.UTF8.GetBytes(sb.ToString());
        }
    }
}
